package com.blb.demo1;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务器端程序
 */
public class MyServer {

    public static final int PORT=8888;

    public void start(){
        System.out.println("服务器启动");
        try(ServerSocket serverSocket = new ServerSocket(PORT)) {
            while (true){
                //接收客户端的连接
                Socket client = serverSocket.accept();
                System.out.println("客户端连接了，ip="+client.getInetAddress());
                try(ObjectInputStream objectInputStream = new ObjectInputStream(client.getInputStream())) {
                    String s = objectInputStream.readUTF();
                    System.out.println(s);
                    try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(client.getOutputStream());){
                        objectOutputStream.writeUTF("收到");
                        objectOutputStream.flush();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }




            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    public static void main(String[] args) {
        MyServer myServer = new MyServer();
        myServer.start();
    }
}
